﻿using KMF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KMF.DAL
{
    public partial class ContactDb
    {
        public static List<ContactListPaggingDto> GetAllContact(int page, int itemsPerPage, ref int totalPage)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Contact_GetAllPaging");
            try
            {
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                SqlParameter itemTotalPage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                itemTotalPage.Direction = ParameterDirection.Output;

                var result = dao.GetDTOList<ContactListPaggingDto>(ref command);
                totalPage = int.Parse(itemTotalPage.Value.ToString() == "" ? "0" : itemTotalPage.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }

        public static List<ContactListPaggingDto> GetAllContactByKeyWordAdmin(int page, int itemsPerPage, ref int totalPage, string keyword)
        {
            var dao = SharedSqlDao;
            var command = dao.GetDbSprocCommand("WSPX_Contact_GetAllByKeyword");
            try
            {
                //var keywords = BuildListKeywordV2(keyword);
                command.Parameters.Add(dao.CreateParameter("@Page", page));
                command.Parameters.Add(dao.CreateParameter("@ItemsPerPage", itemsPerPage));
                command.Parameters.Add(dao.CreateParameter("@Condition", keyword));
                SqlParameter outTotalpage = command.Parameters.Add("@TotalPage", SqlDbType.Int);
                outTotalpage.Direction = ParameterDirection.Output;
                var result = dao.GetDTOList<ContactListPaggingDto>(ref command);
                totalPage = int.Parse(outTotalpage.Value.ToString() == "" ? "0" : outTotalpage.Value.ToString());
                return result;
            }
            finally
            {
                command.Connection.Close();
            }
        }
    }
}
